package scau.erps.view.base;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import scau.erps.app.Erps;
import scau.erps.entity.Factory;
import scau.erps.entity.Line;

/**
 * 生产线转产对话框
 * @author outsider
 *
 */
public class SwitchLineDialog extends LineDialog{
	private static final long serialVersionUID = 1L;
	private List<Factory> listFactory=factoryService.getFactoyByTeamId(team_Id);
	private List<Line>    listLine=lineService.getLineByTeamId(team_Id);
	private DecisionPanel superPanel;
	private static final int[] SWITCHCOST={0,2,6,0}; //转产费用
	public SwitchLineDialog(final DecisionPanel superPanel){
		this.setTitle("生产线转产");
		this.okButton.setText("确定转产");
		this.superPanel=superPanel;
		rentFactoryButton.setVisible(false);
        //获取该小组可转产生产线
		for (int i=0;i<=7;i++){
			typeComboBox[i].setEnabled(false);
			productComboBox[i].setEnabled(false);
			String status=listLine.get(i).getStatus();
			String type=listLine.get(i).getType();
			if (status.equals("全新")||status.equals("空闲"))
				productComboBox[i].setEnabled(true);
			if (type.equals("柔性线"))                          //柔性线无需转产
				productComboBox[i].setEnabled(false);
		}
		//确认与更新按钮的触发器
		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				okButtonAction();
			}
		});
		updateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				updateButtonAction();
			}
		});
		usuriousLoanButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
                new UsuriousLoanDialog(superPanel);
			}
		});
	}
	private void okButtonAction(){
		int count=0;
		int pay=0;
		int cash=Erps.getSand().getCash();
		List<Integer> preSwitch=new ArrayList<Integer>();
		for (int i=0;i<=7;i++){
			if (productComboBox[i].isEnabled()){
				if (productComboBox[i].getSelectedIndex()==0){   //不可选择”无“作为产品类型
					JOptionPane.showMessageDialog(null, "请为转产生产线指定产品类型", "提示",JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				if (PRODUCT[productComboBox[i].getSelectedIndex()].equals(listLine.get(i).getProduct())==false){ //产品类型发生改变 该
        		    	count++;
        		        pay=pay+SWITCHCOST[typeComboBox[i].getSelectedIndex()-1];          //计算转产总费用
        		        preSwitch.add(i);
        		    }	
			    }
			}
		if (cash<pay){
			JOptionPane.showMessageDialog(null, "当前现金不足以支付转产费用，请修改转产计划或进行融资操作", "提示",JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		for (int i:preSwitch){
			lineService.switchLine(team_Id, i+1, PRODUCT[productComboBox[i].getSelectedIndex()]);
		}
		JOptionPane.showMessageDialog(null, "本季度计划转产"+count+"条生产线，支付转产费用 "+pay+"M\n请点击支付费用按钮完成转产", "提示",JOptionPane.INFORMATION_MESSAGE);
    	okButton.setEnabled(false);
    	updateButton.setEnabled(true);
	}
	private void updateButtonAction(){
		int tot=lineService.updateSwitchLine(team_Id);
    	JOptionPane.showMessageDialog(null, "本季度完成转产"+tot+"条"+"\n请更新实物沙盘生产线产品牌并取现金放入转产费用区域", "提示",JOptionPane.INFORMATION_MESSAGE);
		superPanel.getDashBoardPanel().setFactoryData();
		superPanel.getDashBoardPanel().setDashBoardData(16);
    	dispose();
    }
	

}
